#include "QtBox2DCollideItem.h"
#include "QtBox2dAbstractItem.h"
#include <qpainter>
#include <QGraphicsSceneMouseEvent>
#include <qDebug>

QtBox2DCollideItem::QtBox2DCollideItem(QtBox2DScene* scene, QGraphicsItem * parent, float radius) :
	QtBox2DBaseItem(scene, parent),
	mRadius(radius)
{
	generateName(scene);
	setFlag(QGraphicsItem::ItemIsMovable, true);
}

QtBox2DCollideItem::~QtBox2DCollideItem(void)
{
}

void QtBox2DCollideItem::setProperty(const QString &propertyName, const QVariant &value)
{
	emit movedSignal(this, value.toPointF());

}

QRectF QtBox2DCollideItem::boundingRect() const
{
	return QRectF(-mRadius, -mRadius, mRadius*2, mRadius*2);
}

void QtBox2DCollideItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget/* = 0*/)
{
	QPainterPath path;
	path.addEllipse(QPointF(0,0), mRadius, mRadius);
	painter->fillPath(path, Qt::red);
}

void QtBox2DCollideItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
{
        QAbstractGraphicsShapeItem::mouseMoveEvent(event);

	if (event->buttons() == Qt::LeftButton)
	{
		QPointF mousePt = event->scenePos();
		emit movedSignal(this, mousePt);
	}
}

void	QtBox2DCollideItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
{
	{
		QtBox2DScene* pScene = dynamic_cast<QtBox2DScene*>(scene());

		qDebug() << QString("QtBox2DCollideItem::mousePressEvent");
		pScene->pushUndoNormal(QStringList(getName()), "pos", event->scenePos());
	}

        QAbstractGraphicsShapeItem::mousePressEvent(event);
}

void    QtBox2DCollideItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	{
		QtBox2DScene* pScene = dynamic_cast<QtBox2DScene*>(scene());

		pScene->pushRedoNormal("pos", event->scenePos());
	}

        QAbstractGraphicsShapeItem::mouseReleaseEvent(event);

}

void QtBox2DCollideItem::generateName(QtBox2DScene* scene)
{
	QtBox2DBaseItem* pParent = dynamic_cast<QtBox2DBaseItem*>(parentItem());
	if (pParent)
	{
		mName = pParent->getName();
	}
}

//=========================================

void	QtBox2DPointCollideItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
{
	{
		QtBox2DScene* pScene = dynamic_cast<QtBox2DScene*>(scene());

		qDebug() << QString("QtBox2DPointCollideItem::mousePressEvent");
		pScene->pushUndoNormal(QStringList(getName()), "pos", pos());
	
	}
}

void    QtBox2DPointCollideItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	{
		QtBox2DScene* pScene = dynamic_cast<QtBox2DScene*>(scene());

		pScene->pushRedoNormal("pos", pos());
	
	}

}

void QtBox2DPointCollideItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
{
	if (event->buttons() == Qt::LeftButton)
	{
		QPointF rpt = event->pos() - event->lastPos();
		moveBy(rpt.x(), rpt.y());
		emit movedSignal(this, mProp);
	}
}

void QtBox2DPointCollideItem::setProperty(const QString &propertyName, const QVariant &value)
{
	QObject::setProperty(propertyName.toAscii(), value);

	emit movedSignal(this, mProp);

}
